package com.mangabiz.market.dao.hibernate;

// Start of user code for import

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.mangabiz.market.dao.CrudDao;


// End of user code for import

public abstract class AbstractHibernateDao<E, ID extends Serializable> extends
		HibernateDaoSupport implements CrudDao<E, ID> {


	private Class<E> entityClass;


	public Class<E> getEntityClass() {
		return entityClass;
	}


	public AbstractHibernateDao() {
		// Get the class object from the entity generic type parameter
		@SuppressWarnings("unchecked")
		Class<E> entityTypeClass = (Class<E>) ((java.lang.reflect.ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];

		this.entityClass = entityTypeClass;
	}


	@Override
	public void create(E entity) {
		getHibernateTemplate().persist(entity);
	}


	@Override
	public E findById(ID key) {
		return getHibernateTemplate().get(entityClass, key);
	}


	@Override
	public void update(E entity) {
		getHibernateTemplate().update(entity);
	}


	@Override
	public void delete(E entity) {
		getHibernateTemplate().delete(entity);
	}


	@Override
	public List<E> findAll() {
		return getHibernateTemplate().loadAll(getEntityClass());
	}
}